import Vue from "vue";
import VueRouter from "vue-router";

import Login from "@/views/login/Login";

import Home from "@/views/home/Home";
import Welcome from "@/views/home/pages/Welcome";
import Users from "@/views/home/pages/Users";
import Authority from "@/views/home/authority/Authority";
import Roles from "@/views/home/authority/Roles";
import Category from "@/views/home/category/Category";
import Cateparams from "@/views/home/category/Cateparams";
import Goods from "@/views/home/category/Goods";
import AddGood from "@/views/home/category/goodsChildren/AddGood";
import Order from "@/views/home/order/Order";
import Report from "@/views/home/report/Report";
Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/login" },
  { path: "/login", component: Login },
  {
    path: "/home",
    component: Home,
    redirect: "/home/welcome",
    children: [
      { path: "/home/welcome", component: Welcome },
      { path: "/home/users", component: Users },
      { path: "/home/rights", component: Authority },
      { path: "/home/roles", component: Roles },
      { path: "/home/categories", component: Category },
      { path: "/home/params", component: Cateparams },
      { path: "/home/goods", component: Goods },
      { path: "/home/goods/add", component: AddGood },
      { path: "/home/orders", component: Order },
      { path: "/home/reports", component: Report }
    ]
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  //若访问的是登录页面则放行
  if (to.path == "/login") next();
  let token = sessionStorage.getItem("token");
  //若访问其他页面 且没有token 则强制跳转到登录页面
  if (!token) next("/login");
  next();
});

export default router;
